package ace.module.mail.address.core.impl.service.member.impl;

import ace.cmp.core.model.PageResultDto;
import ace.cmp.core.model.R;

import ace.module.common.api.constant.SystemRoleConstants;
import ace.module.common.api.model.input.TenantAndAppTypeInput;
import ace.module.common.api.model.input.TenantInput;
import ace.module.common.api.model.input.TenantPageInput;
import ace.module.mail.address.api.MadMailAddressApi;
import ace.module.mail.address.api.model.dto.MadMailAddressDto;
import ace.module.mail.address.api.model.input.MadAppOutInfoInput;
import ace.module.mail.address.api.model.input.mailaddress.MadMailAddressAddInput;
import ace.module.mail.address.api.model.input.mailaddress.MadMailAddressAddItemInput;
import ace.module.mail.address.api.model.input.mailaddress.MadMailAddressAdminFilterInput;
import ace.module.mail.address.api.model.input.mailaddress.MadMailAddressModifyInput;
import ace.module.mail.address.api.model.input.mailaddress.MadMailAddressModifyItemInput;
import ace.module.mail.address.api.model.input.mailaddress.MadMailAddressSearchInput;
import ace.module.mail.address.core.impl.service.MadMailAddressService;
import ace.module.mail.address.core.impl.service.member.MadMemberMailAddressService;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 会员通行证服务.
 *
 * @author Caspar
 */
@AllArgsConstructor
@Component
public class MadMemberMailAddressServiceImpl implements MadMemberMailAddressService {

  private final MadMailAddressApi madMailAddressApi;
  private final MadMailAddressService madMailAddressService;

  @Override
  public PageResultDto<MadMailAddressDto> adminSearch(
      TenantPageInput<MadMailAddressAdminFilterInput> tenantPageInput) {
    MadMailAddressSearchInput searchInput = MadMailAddressSearchInput.builder()
        .appOutInfo(
            MadAppOutInfoInput.of(
                SystemRoleConstants.MEMBER,
                tenantPageInput.getTenantId()
            )
        )
        .filter(
            MadMailAddressAdminFilterInput.builder()
                .contacts(tenantPageInput.getData().getContacts())
                .accountId(tenantPageInput.getData().getAccountId())
                .contactPhone(tenantPageInput.getData().getContactPhone())
                .type(tenantPageInput.getData().getType())
                .build()
        )
        .page(tenantPageInput.getPage())
        .build();
    return madMailAddressService.search(searchInput);
  }

  @Override
  public Long add(TenantInput<MadMailAddressAddItemInput> input) {
    MadMailAddressAddInput addressAddInput = MadMailAddressAddInput.builder()
        .appOutInfo(
            MadAppOutInfoInput.of(
                SystemRoleConstants.MEMBER,
                input.getTenantId()
            )
        )
        .address(input.getData())
        .build();

    return madMailAddressApi
        .addByAppOutId(addressAddInput)
        .check();
  }

  @Override
  public void modify(TenantInput<MadMailAddressModifyItemInput> input) {
    MadMailAddressModifyInput addressModifyInput = MadMailAddressModifyInput.builder()
        .appOutInfo(
            MadAppOutInfoInput.of(
                SystemRoleConstants.MEMBER,
                input.getTenantId()
            )
        )
        .address(input.getData())
        .build();

    madMailAddressApi
        .modifyByAppOutId(addressModifyInput)
        .check();
  }

  @Override
  public void remove(List<Long> ids) {
    madMailAddressApi.remove(ids);
  }

  @Override
  public MadMailAddressDto get(TenantInput<Long> input) {
    TenantAndAppTypeInput<Long> tenantAndAppTypeInput = TenantAndAppTypeInput.<Long>builder()
        .tenantId(input.getTenantId())
        .type(SystemRoleConstants.MEMBER)
        .data(input.getData())
        .build();
    R<MadMailAddressDto> mailAddressDtoR = madMailAddressApi.getByTenantAndAppTypeAndId(tenantAndAppTypeInput);

    return mailAddressDtoR.check();
  }
}